package com.kk.transactional.controller;

import com.kk.transactional.service.impl.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  声明式事务
 * </p>
 *
 * @author little_lunatic
 * @since 2025-06-05
 */
@Controller
@RequestMapping("/order")
@Api("demo-control")
public class DemoController {

    @Autowired
    private OrderServiceImpl orderService;


    /**
     * 全部成功或全部失败
     */
    @ApiOperation("REQUIRED")
    @PostMapping("REQUIRED")
    public void demo1() {
        orderService.add();
    }

    /**
     * 全部成功或全部失败
     */
    @ApiOperation("SUPPORTS")
    @PostMapping("SUPPORTS")
    public void demo2() {
        orderService.add2();
    }

    /**
     * 全部成功或全部失败
     */
    @ApiOperation("MANDATORY")
    @PostMapping("MANDATORY")
    public void demo3() {
        orderService.add3();
    }

    /**
     *
     */
    @ApiOperation("REQUIRES_NEW")
    @PostMapping("REQUIRES_NEW")
    public void demo4() {
        orderService.add4();
    }

    /**
     * 内外层事务相互独立
     */
    @ApiOperation("REQUIRES_NEW2")
    @PostMapping("REQUIRES_NEW2")
    public void demo44() {
        orderService.add44();
    }

    /**
     * 以非事务方式运行
     */
    @ApiOperation("NOT_SUPPORTED")
    @PostMapping("NOT_SUPPORTED")
    public void demo5() {
        orderService.add5();
    }

    /**
     * 以非事务方式运行，存在事务抛异常
     */
    @ApiOperation("NEVER")
    @PostMapping("NEVER")
    public void demo6() {
        orderService.add6();
    }

    /**
     * 存在事务嵌套运行，不存在就新建
     *
     * 内层影响外层，外层不影响内层
     */
    @ApiOperation("NESTED")
    @PostMapping("NESTED")
    public void demo7() {
        orderService.add7();
    }
    @ApiOperation("NESTED2")
    @PostMapping("NESTED2")
    public void demo8() {
        orderService.add8();
    }
}
